package org.zn.note.common.solution;

import java.io.*;
import java.util.Random;

/**
 * 题目：文本中1个亿的Long型数据，怎么查找，毫秒级
 * <p>
 * 估算文件大小：最多差不多2G
 * Long 取值范围 [9223372036854775807, -9223372036854775808]，算上回车，按可见字符串写入，最多20个字符（可能有个回车符）
 * 100个Long.Max，大概是1.95k： 1.95*1000/100/20，这样差不多1个字符1个byte。
 * 按100个2k算，1亿个是2000000k=2g。
 */
public class BigFileSearch {

    private static final String FILE_PATH = "d:/BigFileSearch.txt";

    // 将1亿个long型数据(转成可见的String)写入到文件里
    private void writeFileTypeLong() throws IOException {
        int size = 100000000; // 1亿
        Random r = new Random();
        FileOutputStream out = new FileOutputStream(FILE_PATH);
        while (size-- > 0)
            out.write((r.nextLong() + "\n").getBytes());
        out.flush();
        out.close();
    }

    private boolean search(Long l) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(FILE_PATH));
        String s;
        while ((s=reader.readLine()) != null)
            if(s.equals(l))
                return true;
        return false;
    }

    public static void main(String[] args) throws IOException {
        BigFileSearch bigFileSearch = new BigFileSearch();
//        bigFileSearch.writeFileTypeLong();
    }
}
